package org.example.lethear.repository;

import org.example.lethear.entity.Post;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;

/**
 * 帖子数据访问接口
 */
@Repository
public interface PostRepository extends JpaRepository<Post, Long> {
    
    /**
     * 根据用户ID查找帖子
     */
    List<Post> findByUserId(Long userId);
    
    /**
     * 根据用户ID分页查找帖子
     */
    Page<Post> findByUserId(Long userId, Pageable pageable);
    
    /**
     * 根据用户ID和状态查找帖子
     */
    Page<Post> findByUserIdAndStatus(Long userId, Integer status, Pageable pageable);
    
    /**
     * 根据分类ID查找帖子
     */
    Page<Post> findByCategoryId(Long categoryId, Pageable pageable);
    
    /**
     * 根据分类ID和状态查找帖子
     */
    Page<Post> findByCategoryIdAndStatus(Long categoryId, Integer status, Pageable pageable);
    
    /**
     * 根据状态查找帖子
     */
    Page<Post> findByStatus(Integer status, Pageable pageable);
    
    /**
     * 查找已发布的帖子
     */
    @Query("SELECT p FROM Post p WHERE p.status = 1 ORDER BY p.createTime DESC")
    Page<Post> findPublishedPosts(Pageable pageable);
    
    /**
     * 查找置顶帖子
     */
    @Query("SELECT p FROM Post p WHERE p.isTop = 1 AND p.status = 1 ORDER BY p.createTime DESC")
    List<Post> findTopPosts();
    
    /**
     * 查找热门帖子（根据点赞数和评论数）
     */
    @Query("SELECT p FROM Post p WHERE p.status = 1 ORDER BY (p.likeCount + p.commentCount * 2) DESC")
    Page<Post> findHotPosts(Pageable pageable);
    
    /**
     * 搜索帖子（标题和内容）
     */
    @Query("SELECT p FROM Post p WHERE p.status = 1 AND (p.title LIKE %:keyword% OR p.content LIKE %:keyword% OR p.summary LIKE %:keyword%)")
    Page<Post> searchPosts(@Param("keyword") String keyword, Pageable pageable);
    
    /**
     * 根据标题搜索帖子
     */
    @Query("SELECT p FROM Post p WHERE p.status = 1 AND p.title LIKE %:keyword%")
    Page<Post> searchByTitle(@Param("keyword") String keyword, Pageable pageable);
    
    /**
     * 在指定分类中搜索帖子
     */
    @Query("SELECT p FROM Post p WHERE p.status = 1 AND p.categoryId = :categoryId AND (p.title LIKE %:keyword% OR p.content LIKE %:keyword% OR p.summary LIKE %:keyword%)")
    Page<Post> searchPostsInCategory(@Param("keyword") String keyword, @Param("categoryId") Long categoryId, Pageable pageable);
    
    /**
     * 搜索指定作者的帖子
     */
    @Query("SELECT p FROM Post p WHERE p.status = 1 AND p.userId = :authorId AND (p.title LIKE %:keyword% OR p.content LIKE %:keyword% OR p.summary LIKE %:keyword%)")
    Page<Post> searchPostsByAuthor(@Param("keyword") String keyword, @Param("authorId") Long authorId, Pageable pageable);
    
    /**
     * 查找指定时间段内的帖子
     */
    @Query("SELECT p FROM Post p WHERE p.status = 1 AND p.createTime BETWEEN :startTime AND :endTime")
    Page<Post> findPostsByCreateTimeBetween(@Param("startTime") LocalDateTime startTime, 
                                           @Param("endTime") LocalDateTime endTime, 
                                           Pageable pageable);
    
    /**
     * 查找用户关注的人发布的帖子
     */
    @Query("SELECT p FROM Post p WHERE p.userId IN (SELECT f.followingId FROM Follow f WHERE f.followerId = :userId) AND p.status = 1 ORDER BY p.createTime DESC")
    Page<Post> findFollowingPosts(@Param("userId") Long userId, Pageable pageable);
    
    /**
     * 增加帖子浏览量
     */
    @Modifying
    @Query("UPDATE Post p SET p.viewCount = p.viewCount + 1 WHERE p.id = :postId")
    int incrementViewCount(@Param("postId") Long postId);
    
    /**
     * 增加帖子点赞数
     */
    @Modifying
    @Query("UPDATE Post p SET p.likeCount = p.likeCount + 1 WHERE p.id = :postId")
    int incrementLikeCount(@Param("postId") Long postId);
    
    /**
     * 减少帖子点赞数
     */
    @Modifying
    @Query("UPDATE Post p SET p.likeCount = p.likeCount - 1 WHERE p.id = :postId AND p.likeCount > 0")
    int decrementLikeCount(@Param("postId") Long postId);
    
    /**
     * 增加帖子评论数
     */
    @Modifying
    @Query("UPDATE Post p SET p.commentCount = p.commentCount + 1 WHERE p.id = :postId")
    int incrementCommentCount(@Param("postId") Long postId);
    
    /**
     * 减少帖子评论数
     */
    @Modifying
    @Query("UPDATE Post p SET p.commentCount = p.commentCount - 1 WHERE p.id = :postId AND p.commentCount > 0")
    int decrementCommentCount(@Param("postId") Long postId);
    
    /**
     * 统计用户发布的帖子数
     */
    @Query("SELECT COUNT(p) FROM Post p WHERE p.userId = :userId AND p.status = 1")
    long countByUserId(@Param("userId") Long userId);
    
    /**
     * 统计分类下的帖子数
     */
    @Query("SELECT COUNT(p) FROM Post p WHERE p.categoryId = :categoryId AND p.status = 1")
    long countByCategoryId(@Param("categoryId") Long categoryId);
    
    /**
     * 统计今日发布的帖子数
     */
    @Query("SELECT COUNT(p) FROM Post p WHERE p.createTime >= :startOfDay AND p.status = 1")
    long countTodayPosts(@Param("startOfDay") LocalDateTime startOfDay);
    
    /**
     * 查找最受欢迎的帖子（根据浏览量）
     */
    @Query("SELECT p FROM Post p WHERE p.status = 1 ORDER BY p.viewCount DESC")
    Page<Post> findMostViewedPosts(Pageable pageable);
    
    /**
     * 查找最新评论的帖子
     */
    @Query("SELECT p FROM Post p WHERE p.status = 1 ORDER BY p.updateTime DESC")
    Page<Post> findRecentlyCommentedPosts(Pageable pageable);
    
    /**
     * 根据多个状态查找帖子
     */
    @Query("SELECT p FROM Post p WHERE p.status IN :statuses")
    Page<Post> findByStatusIn(@Param("statuses") List<Integer> statuses, Pageable pageable);
    
    /**
     * 根据状态统计帖子数量
     */
    long countByStatus(Integer status);
} 